'''
直接把杨辉三角描述出来就可以了！
num_rows从0开始，0就是【】，所以其实是从1开始
'''
class Solution:
    def generate(self, num_rows):
        triangle = []
        for row_num in range(num_rows):
            # The first and last row elements are always 1.
            row = [None for _ in range(row_num+1)]
            row[0], row[-1] = 1, 1
            # Each triangle element is equal to the sum of the elements
            # above-and-to-the-left and above-and-to-the-right.
            for j in range(1, len(row)-1):
                row[j] = triangle[row_num-1][j-1] + triangle[row_num-1][j]
            triangle.append(row)
        return triangle
'''
直接解思路简单效率更高
1. 首位都是1
2. 下一行的数等于上一行的这个位置和这个位置-1的数的和
'''
class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        res  = []
        for i in range(numRows):
            row = [None for _ in range(i+1)]
            row[0],row[-1] = 1,1
            # 只有在第三行i=2的时候才会触发！
            for j in range(1,i):
                row[j] = res[i-1][j-1] + res[i-1][j]
            res.append(row)
        return res
'''
数学解法就是移位相加！使用zip
逐一需要判断空的情况！
'''
class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        if numRows == 0: return []
        res = [[1]]
        while len(res) < numRows:
            # zip把数组旋转之后迭代输出旋转的值，然后把这些值相加之后放进数组就可以了！
            newRow = [a+b for a, b in zip([0]+res[-1], res[-1]+[0])]
            res.append(newRow)
        return res


class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        res = [[1]]
        while len(res) < numRows:
            row = [i + j for i, j in zip([0] + res[-1], res[-1] + [0])]
            res.append(row)
        return res
